/*
 * AGENT SHERPA
 *  
 * Assignement 2 of the Agent Oriented Software Engineering Course
 * M.Sc. in Advanced Software Engineering (Part-time)
 * University College Dublin www.ucd.ie
 *
 * Copyright (c) 2007 Chris Walsh, Aidan Morrisy and Iain Hull.
 * All rights reserved.
 * 
 * Filename: Main.aps
 * Author:   iain
 * Date:     06 February 2008
 */

package org.agentsherpa.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Observable;
import java.util.Observer;

import org.agentsherpa.model.Drone;
import org.agentsherpa.model.GlobalMap;
import org.agentsherpa.model.Point;
import org.agentsherpa.model.SimulatorModel;

public class MapComponent extends Component implements Observer {
    private static final long serialVersionUID = -8329989911441325285L;

    private SimulatorModel simulator;
    private int zoom = 20;
    private BufferedImage roadImage;

    public MapComponent(SimulatorModel simulator) {
        this.simulator = simulator;
        this.simulator.addObserver(this);
        roadImage = drawRoadImage(simulator.getMap(), zoom);
    }
    
    private static BufferedImage drawRoadImage(GlobalMap map, int zoom) {
        BufferedImage roadImage = new BufferedImage(map.getWidth() * zoom, map.getHeight() * zoom, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = roadImage.createGraphics();

        for(Point point : map.getRoadPoints()) {
                  int s = Math.max(zoom - 1, 1);
                  g2.setColor(Color.YELLOW);
                  g2.fillRect(point.getX()*zoom, point.getY()*zoom, s, s);                  
        }    
        
        return roadImage;
    }
    
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.drawImage(roadImage, null, 0, 0);
        
        g2.setColor(Color.RED);
        for(Drone drone : simulator.getDrones()) {
            System.out.println("Drawing drone " + drone);
            int size = droneSize();
            java.awt.Point drawLocation = droneLocation(drone.getLocation());
            g2.fillOval(drawLocation.x, drawLocation.y, size, size);
        }
    }

    private java.awt.Point droneLocation(Point location) {
        int size = droneSize();
        int offset = size - zoom;
        int x = location.getX() * zoom - offset ;
        int y = location.getY() * zoom - offset;
        java.awt.Point drawLocation = new java.awt.Point(x, y);
        return drawLocation;
    }
    
    private int droneSize() {
        return Math.max(zoom, 5);
    }

    public void update(Observable o, Object arg) {
        Graphics2D g2 = (Graphics2D) getGraphics();
        
        if (arg instanceof SimulatorModel.DroneChange) {
            SimulatorModel.DroneChange dc = (SimulatorModel.DroneChange)arg;
            Point oldLocation = dc.getOldLocation();
            if (oldLocation != null) {
                int size = droneSize();
                java.awt.Point drawLocation = droneLocation(oldLocation);
                g2.clearRect(drawLocation.x, drawLocation.y, size, size);
            }
        }
            
        paint(g2);
    }
}
